const app = getApp();
var utils = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    currPage: 1,
    loadingMoreHidden: 1,
    startTime: '',
    endTime: '',
    state: 0 // 1 供货订单统计, 2 代卖订单统计
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let title = options.state == 1 ? '供货订单统计' : '代卖订单统计'
    this.setData({
      state: options.state
    });
    wx.setNavigationBarTitle({
      title: title
    })
    this.getTime();
    this.getList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
  },
  getTime: function(){
    var that = this;
    var now = new Date();
    var startNow = new Date();  
    startNow.setMonth(startNow.getMonth()-1);  //开始时间
    that.setData({
      startTime: that.getShortDate(startNow),
      endTime: that.getShortDate(now)
    })
  },
  //短日期格式
   getShortDate: function(date){
     var month = date.getMonth() + 1;
     month = month < 10 ? '0' + month : month;
    return date.getFullYear() + '-' + month+ '-' + date.getDate();
   },
   bindStartTime: function(e) {
    this.setData({
      startTime: e.detail.value
    })
    this.getList();
  },
    //月报
    bindEndTime: function(e) {
      this.setData({
        endTime: e.detail.value
      })
      this.getList();
    },
  //获取已确认列表
  getList: function(status) {
    let that = this;  
    wx.request({
      url: app.globalData.host + '/api/supply/OrderReport',
      data:{
        saleUid: app.globalData.userInfo.Uid,
        state: that.data.state,
        startTime: that.data.startTime,
        endTime: that.data.endTime,
        page: that.data.currPage
      },
      success: function(res) {
        if (res.data.Code == 0) {
          let list = res.data.Data;
          if (that.data.currPage != 1) {
            var preList = that.data.lists
            list = preList.concat(list);
          }
          that.setData({
            list: list
          })
        }
      }
    })
  },
  // 加载更多
  loadMore: function(e) {
    var that = this;
    if (that.data.loadingMoreHidden > 1 && that.data.currPage < that.data.loadingMoreHidden) {
      var currPage = that.data.currPage;
      that.setData({
        currPage: currPage + 1
      })
      that.getList();
    }
  },
})